package com.twu.biblioteca.client;

import com.twu.biblioteca.IOStream.ConsoleIO;
import com.twu.biblioteca.IOStream.IO;
import com.twu.biblioteca.book.Books;
import com.twu.biblioteca.book.BooksFactory;
import com.twu.biblioteca.commands.*;
import com.twu.biblioteca.customer.Customers;
import com.twu.biblioteca.customer.CustomersFactory;
import com.twu.biblioteca.library.*;
import com.twu.biblioteca.movie.Movies;
import com.twu.biblioteca.movie.MoviesFactory;

import java.io.IOException;


public class Client {

    private Library library;

    public Client() {
        Books books = BooksFactory.createBooks();
        Movies movies = MoviesFactory.createMovies();
        Customers customers = CustomersFactory.createCustomers();
        AllCommands commands = CommandsFactory.createCommands();
        IO io = new ConsoleIO();
        this.library = new Library(books, movies, customers, commands, io);
    }

    public void run() throws IOException {
        ICommand currentCommand;
        do {
            library.getIo().outputCommand(library.startMessage());
            currentCommand = library.getIo().inputCommand();

            if (library.isValidCommand(currentCommand)) {
                currentCommand.execute(library);
                if(library.isQuitCommand(currentCommand))
                    break;
            }
            else {
                library.getIo().outputCommand(library.invalidOptionMessage());
            }
        } while(true);
    }
}